Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 583)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.97608 12.97513 12.97423 12.97336 12.97251 12.97169 12.97089 12.97010
##   [9] 12.96931 12.96853 12.96774 12.96695 12.96614 12.96531 12.96446 12.96358
##  [17] 12.96266 12.96171 12.96071 12.95966 12.95855 12.95739 12.95616 12.95486
##  [25] 12.95348 12.95202 12.95048 12.94884 12.94711 12.94529 12.94338 12.94140
##  [33] 12.93934 12.93723 12.93505 12.93283 12.93057 12.92827 12.92594 12.92358
##  [41] 12.92121 12.91883 12.91645 12.91407 12.91170 12.90935 12.90702 12.90472
##  [49] 12.90246 12.90024 12.89807 12.89596 12.89391 12.89193 12.89003 12.88821
##  [57] 12.88648 12.88473 12.88286 12.88088 12.87879 12.87660 12.87433 12.87198
##  [65] 12.86956 12.86708 12.86455 12.86198 12.85938 12.85675 12.85411 12.85147
##  [73] 12.84883 12.84620 12.84360 12.84102 12.83849 12.83600 12.83358 12.83122
##  [81] 12.82894 12.82675 12.82465 12.82265 12.82077 12.81901 12.81738 12.81589
##  [89] 12.81455 12.81337 12.81236 12.81152 12.81029 12.80814 12.80514 12.80137
##  [97] 12.79690 12.79182 12.78619 12.78010 12.77362 12.76683 12.75980 12.75261
## [105] 12.74534 12.73806 12.73085 12.72379 12.71695 12.71040 12.70423 12.69851
## [113] 12.69332 12.68873 12.68482 12.68166 12.67934 12.67793 12.67750 12.67813
## [121] 12.67976 12.68222 12.68547 12.68943 12.69406 12.69928 12.70504 12.71129
## [129] 12.71795 12.72498 12.73231 12.73988 12.74763 12.75551 12.76345 12.77139
## [137] 12.77928 12.78705 12.79465 12.80201 12.80908 12.81579 12.82424 12.83616
## [145] 12.85099 12.86819 12.88719 12.90743 12.92835 12.94939 12.97000 12.98961
## [153] 13.00766 13.02361 13.03687 13.04691 13.05621 13.06749 13.08049 13.09496
## [161] 13.11064 13.12729 13.14465 13.16247 13.18049 13.19847 13.21615 13.23327
## [169] 13.24959 13.26486 13.27882 13.29121 13.30179 13.31030 13.31649 13.32214
## [177] 13.32905 13.33708 13.34605 13.35578 13.36612 13.37689 13.38792 13.39905
## [185] 13.41010 13.42091 13.43131 13.44113 13.45020 13.45835 13.46541 13.47122
## [193] 13.47560 13.47839 13.47942 13.47851 13.47551 13.47024 13.46280 13.45351
## [201] 13.44250 13.42992 13.41592 13.40062 13.38416 13.36670 13.34837 13.32931
## [209] 13.30967 13.28957 13.26917 13.24860 13.22800 13.20752 13.18730 13.16747
## [217] 13.14818 13.12647 13.09981 13.06896 13.03471 12.99781 12.95904 12.91917
## [225] 12.87897 12.83921 12.80065 12.76407 12.73023 12.69991 12.67388 12.64813
## [233] 12.61853 12.58567 12.55013 12.51249 12.47335 12.43328 12.39288 12.35273
## [241] 12.31342 12.27552 12.23964 12.20634 12.17623 12.14988 12.12788 12.10894
## [249] 12.09131 12.07486 12.05946 12.04499 12.03132 12.01834 12.00591 11.99391
## [257] 11.98221 11.97069 11.95923 11.94770 11.93597 11.92572 11.91849 11.91396
## [265] 11.91182 11.91173 11.91338 11.91644 11.92060 11.92553 11.93091 11.93642
## [273] 11.94174 11.94654 11.95051 11.95333 11.95466 11.95419 11.95161 11.94658
## [281] 11.94111 11.93724 11.93469 11.93319 11.93246 11.93223 11.93222 11.93217
## [289] 11.93179 11.93081 11.92896 11.92596 11.92154 11.91542 11.90799 11.89984
## [297] 11.89103 11.88161 11.87161 11.86109 11.85009 11.83867 11.82688 11.81475
## [305] 11.80234 11.78969 11.77686 11.76389 11.75082 11.73772 11.72462 11.71157
## [313] 11.69863 11.68583 11.67323 11.66088 11.64882 11.63710 11.62577 11.61487
## [321] 11.60446 11.59458 11.58272 11.56673 11.54728 11.52500 11.50054 11.47454
## [329] 11.44765 11.42051 11.39375 11.36804 11.34400 11.32229 11.30355 11.28842
## [337] 11.27754 11.27157 11.26776 11.26313 11.25794 11.25245 11.24694 11.24166
## [345] 11.23687 11.23285 11.22985 11.22813 11.22796 11.22961 11.23334 11.23940
## [353] 11.24818 11.25973 11.27381 11.29019 11.30864 11.32894 11.35086 11.37416
## [361] 11.39861 11.42398 11.45005 11.47658 11.50335 11.53012 11.55667 11.58275
## [369] 11.60816 11.63264 11.65599 11.67795 11.70167 11.72996 11.76216 11.79759
## [377] 11.83557 11.87544 11.91652 11.95813 11.99960 12.04026 12.07944 12.11645
## [385] 12.15064 12.18131 12.20780 12.23371 12.26280 12.29466 12.32885 12.36495
## [393] 12.40253 12.44118 12.48047 12.51997 12.55925 12.59790 12.63549 12.67159
## [401] 12.70578 12.73764 12.76674 12.79265 12.81495 12.83323 12.84951 12.86601
## [409] 12.88256 12.89900 12.91515 12.93085 12.94593 12.96021 12.97354 12.98574
## [417] 12.99664 13.00607 13.01387 13.01987 13.02322 13.02342 13.02083 13.01578
## [425] 13.00863 12.99973 12.98943 12.97808 12.96601 12.95360 12.94117 12.92909
## [433] 12.91769 12.90734 12.89837 12.89114 12.88270 12.87027 12.85446 12.83588
## [441] 12.81515 12.79286 12.76962 12.74605 12.72276 12.70034 12.67942 12.66060
## [449] 12.64448 12.63168 12.61955 12.60519 12.58889 12.57091 12.55153 12.53102
## [457] 12.50965 12.48770 12.46543 12.44312 12.42104 12.39946 12.37866 12.35890
## [465] 12.34046 12.32361 12.30863 12.29577 12.28533 12.27592 12.26608 12.25594
## [473] 12.24563 12.23526 12.22498 12.21491 12.20518 12.19591 12.18723 12.17927
## [481] 12.17217 12.16603 12.16101 12.15680 12.15303 12.14971 12.14686 12.14447
## [489] 12.14255 12.14111 12.14016 12.13970 12.13974 12.14029 12.14136 12.14294
## [497] 12.14506 12.14770 12.15090 12.15542 12.16192 12.17018 12.18002 12.19120
## [505] 12.20354 12.21683 12.23086 12.24541 12.26030 12.27531 12.29023 12.30486
## [513] 12.31899 12.33243 12.34495 12.35635 12.36644 12.37500 12.38332 12.39272
## [521] 12.40305 12.41414 12.42585 12.43801 12.45047 12.46307 12.47565 12.48806
## [529] 12.50013 12.51172 12.52266 12.53280 12.54198 12.55005 12.55758 12.56526
## [537] 12.57307 12.58098 12.58897 12.59702 12.60511 12.61321 12.62132 12.62939
## [545] 12.63742 12.64538 12.65325 12.66101 12.66864 12.67611 12.68340 12.69049
## [553] 12.69737 12.70400 12.71037 12.71653 12.72256 12.72845 12.73420 12.73983
## [561] 12.74532 12.75068 12.75591 12.76101 12.76599 12.77084 12.77557 12.78017
## [569] 12.78466 12.78902 12.79326 12.79739 12.80140 12.80529 12.80908 12.81274
## [577] 12.81630 12.81975 12.82309 12.82632 12.82945 12.83247 12.83539
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 583)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.60822 12.60454 12.60097 12.59749 12.59411 12.59082 12.58761 12.58449
##   [9] 12.58145 12.57848 12.57559 12.57277 12.57001 12.56731 12.56467 12.56209
##  [17] 12.55956 12.55707 12.55463 12.55223 12.54987 12.54754 12.54524 12.54296
##  [25] 12.54071 12.53847 12.53625 12.53404 12.53184 12.52964 12.52743 12.52523
##  [33] 12.52304 12.52087 12.51871 12.51658 12.51449 12.51242 12.51041 12.50843
##  [41] 12.50652 12.50466 12.50286 12.50114 12.49949 12.49793 12.49645 12.49506
##  [49] 12.49377 12.49258 12.49150 12.49054 12.48970 12.48898 12.48840 12.48795
##  [57] 12.48764 12.48741 12.48719 12.48700 12.48683 12.48669 12.48658 12.48651
##  [65] 12.48649 12.48652 12.48660 12.48675 12.48695 12.48723 12.48759 12.48803
##  [73] 12.48855 12.48916 12.48987 12.49068 12.49159 12.49262 12.49376 12.49503
##  [81] 12.49642 12.49794 12.49960 12.50141 12.50336 12.50546 12.50772 12.51014
##  [89] 12.51273 12.51549 12.51843 12.52155 12.52466 12.52757 12.53031 12.53289
##  [97] 12.53534 12.53767 12.53991 12.54207 12.54418 12.54625 12.54831 12.55037
## [105] 12.55246 12.55460 12.55680 12.55909 12.56148 12.56401 12.56667 12.56951
## [113] 12.57253 12.57576 12.57922 12.58292 12.58690 12.59116 12.59573 12.60062
## [121] 12.60585 12.61137 12.61717 12.62323 12.62952 12.63604 12.64276 12.64965
## [129] 12.65671 12.66391 12.67122 12.67864 12.68614 12.69370 12.70130 12.70893
## [137] 12.71656 12.72417 12.73175 12.73926 12.74863 12.76141 12.77706 12.79505
## [145] 12.81485 12.83592 12.85773 12.87975 12.90143 12.92225 12.94167 12.95916
## [153] 12.97418 12.98620 12.99861 13.01477 13.03413 13.05614 13.08023 13.10585
## [161] 13.13243 13.15943 13.18629 13.21244 13.23732 13.26039 13.28109 13.29885
## [169] 13.31311 13.32333 13.33163 13.34044 13.34967 13.35922 13.36900 13.37890
## [177] 13.38884 13.39871 13.40842 13.41787 13.42696 13.43560 13.44370 13.45115
## [185] 13.45786 13.46372 13.46866 13.47256 13.47533 13.47688 13.47711 13.47592
## [193] 13.47321 13.46889 13.46286 13.45503 13.44447 13.43058 13.41375 13.39437
## [201] 13.37281 13.34946 13.32471 13.29894 13.27254 13.24588 13.21936 13.19335
## [209] 13.16825 13.14443 13.12228 13.10219 13.08055 13.05403 13.02340 12.98946
## [217] 12.95297 12.91473 12.87551 12.83610 12.79727 12.75982 12.72452 12.69215
## [225] 12.66350 12.63935 12.61617 12.59019 12.56176 12.53126 12.49905 12.46551
## [233] 12.43100 12.39589 12.36056 12.32536 12.29068 12.25687 12.22431 12.19337
## [241] 12.16441 12.13781 12.11393 12.09315 12.07583 12.06146 12.04914 12.03862
## [249] 12.02964 12.02197 12.01536 12.00955 12.00431 11.99938 11.99452 11.98948
## [257] 11.98401 11.97787 11.97082 11.96513 11.96298 11.96392 11.96751 11.97330
## [265] 11.98085 11.98971 11.99944 12.00960 12.01974 12.02942 12.03819 12.04561
## [273] 12.05124 12.05463 12.05533 12.05689 12.06265 12.07189 12.08386 12.09786
## [281] 12.11315 12.12900 12.14468 12.15947 12.17264 12.18346 12.19120 12.19514
## [289] 12.19454 12.19104 12.18679 12.18183 12.17621 12.16998 12.16319 12.15589
## [297] 12.14814 12.13996 12.13143 12.12258 12.11347 12.10414 12.09464 12.08503
## [305] 12.07535 12.06564 12.05597 12.04638 12.03466 12.01902 12.00007 11.97845
## [313] 11.95479 11.92972 11.90385 11.87783 11.85229 11.82784 11.80513 11.78477
## [321] 11.76741 11.75366 11.73979 11.72205 11.70105 11.67738 11.65167 11.62451
## [329] 11.59650 11.56826 11.54039 11.51349 11.48817 11.46504 11.44470 11.42775
## [337] 11.41481 11.40648 11.40002 11.39252 11.38426 11.37557 11.36674 11.35807
## [345] 11.34989 11.34247 11.33615 11.33121 11.32796 11.32671 11.32776 11.33142
## [353] 11.33790 11.34703 11.35862 11.37243 11.38824 11.40584 11.42501 11.44551
## [361] 11.46714 11.48968 11.51289 11.53657 11.56049 11.58443 11.60817 11.63150
## [369] 11.65418 11.67600 11.69675 11.71619 11.73757 11.76378 11.79413 11.82790
## [377] 11.86438 11.90285 11.94261 11.98294 12.02313 12.06248 12.10027 12.13578
## [385] 12.16832 12.19716 12.22160 12.24475 12.27002 12.29713 12.32578 12.35569
## [393] 12.38657 12.41815 12.45013 12.48222 12.51415 12.54563 12.57636 12.60607
## [401] 12.63446 12.66126 12.68618 12.70893 12.72922 12.74677 12.76351 12.78138
## [409] 12.80008 12.81933 12.83884 12.85831 12.87746 12.89600 12.91365 12.93010
## [417] 12.94508 12.95829 12.96945 12.97827 12.98511 12.99061 12.99484 12.99789
## [425] 12.99982 13.00073 13.00069 12.99979 12.99810 12.99571 12.99270 12.98914
## [433] 12.98511 12.98070 12.97600 12.97106 12.96427 12.95422 12.94136 12.92615
## [441] 12.90905 12.89049 12.87095 12.85086 12.83069 12.81089 12.79190 12.77418
## [449] 12.75819 12.74438 12.73004 12.71242 12.69191 12.66891 12.64382 12.61703
## [457] 12.58893 12.55992 12.53039 12.50073 12.47135 12.44262 12.41496 12.38875
## [465] 12.36438 12.34226 12.32277 12.30630 12.29327 12.28068 12.26565 12.24864
## [473] 12.23013 12.21062 12.19058 12.17049 12.15084 12.13210 12.11476 12.09930
## [481] 12.08619 12.07593 12.06899 12.06370 12.05813 12.05242 12.04670 12.04110
## [489] 12.03574 12.03075 12.02626 12.02239 12.01929 12.01707 12.01586 12.01579
## [497] 12.01699 12.01959 12.02372 12.03013 12.03931 12.05096 12.06481 12.08057
## [505] 12.09795 12.11668 12.13645 12.15699 12.17801 12.19923 12.22036 12.24112
## [513] 12.26122 12.28037 12.29829 12.31470 12.32931 12.34183 12.35404 12.36780
## [521] 12.38289 12.39911 12.41626 12.43413 12.45251 12.47121 12.49002 12.50874
## [529] 12.52716 12.54507 12.56228 12.57857 12.59375 12.60762 12.62099 12.63483
## [537] 12.64907 12.66368 12.67860 12.69380 12.70924 12.72485 12.74061 12.75646
## [545] 12.77236 12.78826 12.80412 12.81990 12.83554 12.85101 12.86625 12.88123
## [553] 12.89590 12.91021 12.92411 12.93774 12.95125 12.96464 12.97791 12.99107
## [561] 13.00412 13.01706 13.02989 13.04263 13.05527 13.06781 13.08027 13.09263
## [569] 13.10491 13.11711 13.12923 13.14127 13.15324 13.16514 13.17698 13.18875
## [577] 13.20046 13.21211 13.22371 13.23526 13.24676 13.25821 13.26962
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 583)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.95235 11.95085 11.94942 11.94806 11.94676 11.94552 11.94432 11.94317
##   [9] 11.94205 11.94095 11.93987 11.93881 11.93775 11.93669 11.93562 11.93454
##  [17] 11.93343 11.93229 11.93112 11.92991 11.92864 11.92732 11.92593 11.92447
##  [25] 11.92294 11.92132 11.91961 11.91780 11.91588 11.91385 11.91170 11.90943
##  [33] 11.90702 11.90447 11.90177 11.89892 11.89592 11.89276 11.88947 11.88606
##  [41] 11.88254 11.87893 11.87524 11.87149 11.86768 11.86384 11.85998 11.85610
##  [49] 11.85223 11.84838 11.84456 11.84079 11.83707 11.83343 11.82988 11.82643
##  [57] 11.82309 11.81988 11.81682 11.81391 11.81118 11.80862 11.80627 11.80413
##  [65] 11.80183 11.79901 11.79573 11.79200 11.78786 11.78336 11.77853 11.77340
##  [73] 11.76801 11.76240 11.75660 11.75065 11.74459 11.73845 11.73227 11.72608
##  [81] 11.71992 11.71383 11.70785 11.70200 11.69633 11.69087 11.68567 11.68074
##  [89] 11.67614 11.67189 11.66804 11.66461 11.66166 11.65920 11.65728 11.65594
##  [97] 11.65521 11.65513 11.65572 11.65660 11.65734 11.65798 11.65855 11.65908
## [105] 11.65961 11.66018 11.66080 11.66153 11.66238 11.66340 11.66461 11.66605
## [113] 11.66776 11.66976 11.67209 11.67479 11.67788 11.68139 11.68537 11.68984
## [121] 11.69485 11.70041 11.70657 11.71335 11.72079 11.72893 11.73903 11.75210
## [129] 11.76775 11.78564 11.80538 11.82662 11.84898 11.87209 11.89559 11.91911
## [137] 11.94228 11.96473 11.98610 12.00601 12.02411 12.04001 12.05721 12.07895
## [145] 12.10451 12.13318 12.16424 12.19698 12.23067 12.26461 12.29806 12.33033
## [153] 12.36068 12.38841 12.41280 12.43313 12.45250 12.47428 12.49814 12.52377
## [161] 12.55085 12.57903 12.60801 12.63746 12.66706 12.69648 12.72540 12.75349
## [169] 12.78044 12.80592 12.82960 12.85116 12.87029 12.88664 12.89991 12.91187
## [177] 12.92441 12.93742 12.95074 12.96426 12.97784 12.99135 13.00465 13.01761
## [185] 13.03010 13.04199 13.05315 13.06344 13.07273 13.08088 13.08778 13.09327
## [193] 13.09724 13.09955 13.10006 13.09865 13.09518 13.08951 13.08100 13.06926
## [201] 13.05458 13.03726 13.01759 12.99586 12.97235 12.94737 12.92120 12.89414
## [209] 12.86647 12.83850 12.81050 12.78277 12.75561 12.72930 12.70414 12.68042
## [217] 12.65843 12.63380 12.60267 12.56610 12.52516 12.48090 12.43439 12.38668
## [225] 12.33884 12.29193 12.24700 12.20513 12.16736 12.13477 12.10840 12.08422
## [233] 12.05771 12.02927 11.99929 11.96813 11.93620 11.90387 11.87152 11.83955
## [241] 11.80833 11.77825 11.74969 11.72304 11.69869 11.67701 11.65839 11.64231
## [249] 11.62786 11.61483 11.60303 11.59226 11.58232 11.57300 11.56413 11.55548
## [257] 11.54687 11.53810 11.52896 11.51926 11.50880 11.49966 11.49382 11.49092
## [265] 11.49061 11.49253 11.49635 11.50170 11.50823 11.51560 11.52344 11.53141
## [273] 11.53916 11.54633 11.55256 11.55752 11.56085 11.56218 11.56119 11.55750
## [281] 11.55412 11.55390 11.55632 11.56081 11.56684 11.57386 11.58132 11.58867
## [289] 11.59538 11.60089 11.60466 11.60614 11.60479 11.60006 11.59229 11.58226
## [297] 11.57016 11.55614 11.54038 11.52305 11.50431 11.48434 11.46329 11.44135
## [305] 11.41867 11.39542 11.37179 11.34792 11.32400 11.30018 11.27664 11.25355
## [313] 11.23107 11.20938 11.18863 11.16900 11.15067 11.13378 11.11853 11.10506
## [321] 11.09356 11.08418 11.07476 11.06325 11.05004 11.03550 11.01999 11.00390
## [329] 10.98760 10.97147 10.95587 10.94118 10.92778 10.91604 10.90634 10.89905
## [337] 10.89454 10.89319 10.89447 10.89749 10.90208 10.90808 10.91531 10.92362
## [345] 10.93283 10.94278 10.95330 10.96423 10.97540 10.98663 10.99777 11.00865
## [353] 11.02074 11.03546 11.05258 11.07185 11.09303 11.11588 11.14016 11.16564
## [361] 11.19207 11.21921 11.24682 11.27466 11.30250 11.33009 11.35718 11.38355
## [369] 11.40895 11.43314 11.45588 11.47693 11.49868 11.52337 11.55056 11.57981
## [377] 11.61066 11.64268 11.67541 11.70841 11.74123 11.77343 11.80456 11.83418
## [385] 11.86183 11.88708 11.90948 11.93107 11.95404 11.97820 12.00335 12.02927
## [393] 12.05576 12.08262 12.10963 12.13661 12.16333 12.18960 12.21522 12.23997
## [401] 12.26365 12.28605 12.30698 12.32623 12.34358 12.35885 12.37376 12.39001
## [409] 12.40727 12.42525 12.44362 12.46208 12.48032 12.49802 12.51488 12.53058
## [417] 12.54482 12.55727 12.56764 12.57561 12.58114 12.58452 12.58599 12.58576
## [425] 12.58406 12.58110 12.57711 12.57231 12.56692 12.56116 12.55526 12.54943
## [433] 12.54390 12.53889 12.53461 12.53130 12.52713 12.52037 12.51139 12.50057
## [441] 12.48827 12.47488 12.46075 12.44626 12.43179 12.41769 12.40436 12.39214
## [449] 12.38143 12.37258 12.36388 12.35347 12.34156 12.32834 12.31399 12.29871
## [457] 12.28269 12.26612 12.24920 12.23211 12.21506 12.19822 12.18180 12.16598
## [465] 12.15097 12.13694 12.12409 12.11261 12.10270 12.09292 12.08188 12.06980
## [473] 12.05693 12.04351 12.02978 12.01598 12.00235 11.98913 11.97656 11.96488
## [481] 11.95433 11.94516 11.93759 11.93038 11.92223 11.91335 11.90392 11.89415
## [489] 11.88424 11.87438 11.86477 11.85561 11.84709 11.83942 11.83279 11.82741
## [497] 11.82346 11.82115 11.82067 11.82176 11.82400 11.82728 11.83154 11.83668
## [505] 11.84262 11.84928 11.85658 11.86443 11.87275 11.88146 11.89047 11.89969
## [513] 11.90906 11.91848 11.92786 11.93714 11.94622 11.95501 11.96421 11.97448
## [521] 11.98570 11.99776 12.01053 12.02392 12.03779 12.05205 12.06656 12.08122
## [529] 12.09591 12.11052 12.12493 12.13903 12.15270 12.16583 12.17883 12.19217
## [537] 12.20586 12.21986 12.23419 12.24881 12.26372 12.27892 12.29437 12.31008
## [545] 12.32604 12.34222 12.35862 12.37522 12.39202 12.40900 12.42616 12.44346
## [553] 12.46092 12.47851 12.49622 12.51408 12.53212 12.55036 12.56878 12.58738
## [561] 12.60618 12.62517 12.64434 12.66371 12.68327 12.70302 12.72297 12.74311
## [569] 12.76345 12.78398 12.80471 12.82563 12.84676 12.86808 12.88960 12.91133
## [577] 12.93325 12.95538 12.97771 13.00024 13.02298 13.04593 13.06908
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")